package com.cy.personer.ceptor;

import com.cy.personer.bean.Log;
import com.cy.personer.bean.Work;
import com.cy.personer.biz.LogBiz;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.condition.RequestConditionHolder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;

@Aspect
@Component
public class OperationCeptor {
    @Resource
    private LogBiz logBizImpl;

    @Around("within(com.cy.personer..*) && @annotation(operationLog)")
    public Object doAroundMethod(ProceedingJoinPoint pjd, OperationLog operationLog) throws Throwable {
//        long startTime=System.currentTimeMillis();//开始时间
        Object result = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Work work = (Work) request.getSession().getAttribute("work");
        Log log = new Log();
        log.setUserId(work.getWorkId());
        log.setUserName(work.getAccount());

//        Object[] params = pjd.getArgs();//获取请求参数
//        System.out.println("监听到传入参数为:");
//        for(Object param:params) {
//            System.out.println(param);
//        }
        //###################上面代码为方法执行前#####################
        result = pjd.proceed();//执行方法，获取返回参数
        //###################下面代码为方法执行后#####################
//        System.out.println("返回参数为:" + result);

//        String user = operationLog.userIndex()==null?operationLog.user():(String)params[operationLog.userIndex()];//操作人
        String operType = operationLog.operType();//操作类型
        String methods = operationLog.methods();
        log.setOperType(operType);
        log.setLogdetail(methods);
        logBizImpl.addLog(log);

//       System.out.println("操作人: " + work.getAccount() +" 操作类型: " + operType);

//        long endTime=System.currentTimeMillis();//结束时间
//        float excTime=(float)(endTime-startTime)/1000;
//        System.out.println("执行时间:"+excTime+"s");
//        System.out.println("#######################分隔符##########################");
        return result;
    }


}
